package com.betterjr.modules.profit.service;

import com.betterjr.common.service.BaseService;
import com.betterjr.mapper.pagehelper.Page;
import com.betterjr.modules.profit.constants.LoanServiceConstant;
import com.betterjr.modules.profit.constants.LoanShareConstant;
import com.betterjr.modules.profit.dao.ScfLoanServiceMapper;
import com.betterjr.modules.profit.entity.ScfLoanService;
import com.betterjr.modules.profit.entity.ScfLoanShare;
import com.betterjr.modules.risk.loanmanage.entity.PaymentPlan;
import com.betterjr.modules.risk.loanmanage.service.PaymentPlanService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * Created by chenlj on 2018/6/27
 */
@Service
public class ScfLoanServiceService extends BaseService<ScfLoanServiceMapper, ScfLoanService> {

    @Autowired
    private ScfLoanShareService scfLoanShareService;

    @Autowired
    private PaymentPlanService paymentPlanService;

    private static final String PAYMENT_TYPE = "2";  //扣款计划为服务费的状态

    private static final String STATUS = "0";         //已扣款的状态

    public Page<ScfLoanService> queryLoanServiceList(Map<String, Object> anMap, int anFlag, int anPageNum, int anPageSize) {

        /**
         * 查找付款计划同步已经付了服务费的数据
         */
        ScfLoanService scfLoanServiceQuery = new ScfLoanService();
        scfLoanServiceQuery.setBusinstatus(LoanServiceConstant.BUSINSTATUS_UNPAID);
        List<ScfLoanService> unpaidLoanServiceList = this.select(scfLoanServiceQuery);
        for (ScfLoanService scfLoanServiceTemp : unpaidLoanServiceList) {
            PaymentPlan paymentPlanQuery = new PaymentPlan();
            paymentPlanQuery.setContractId(scfLoanServiceTemp.getLoanId());
            paymentPlanQuery.setPaymentType(PAYMENT_TYPE);
            List<PaymentPlan> paymentPlanList = paymentPlanService.select(paymentPlanQuery);
            if (!paymentPlanList.isEmpty()) {
                PaymentPlan paymentPlanTemp = paymentPlanList.get(0);
                if (STATUS.equals(paymentPlanTemp.getStatus())) {
                    /**程序改变未收服务费的状态为已收*/
                    String gatheringType = String.valueOf(paymentPlanTemp.getPaymentDate())
                            .replace("-","").substring(0,8);
                    scfLoanServiceTemp.setGatheringType(paymentPlanTemp.getPaymentMode());
                    scfLoanServiceTemp.setGatheringDate(gatheringType);
                    scfLoanServiceTemp.setBusinstatus(LoanServiceConstant.BUSINSTATUS_PREPAID);
                    this.updateByPrimaryKeySelective(scfLoanServiceTemp);
                }
            }
        }

        anMap.remove("flag");
        anMap.remove("pageNum");
        anMap.remove("pageSize");
        anMap.remove("pages");
        anMap.remove("total");

        if (anMap.get("custno")==null || ((String) anMap.get("custno")).equals("")) {
            anMap.remove("custno");
        }
        if (anMap.get("gatheringDate")==null || ((String) anMap.get("gatheringDate")).equals("")) {
            anMap.remove("gatheringDate");
        } else {
            String gatheringDate = ((String) anMap.get("gatheringDate")).replace("-","").replace(",","");
            anMap.remove("gatheringDate");
            String startTime = gatheringDate.substring(0,8);
            String endTime = gatheringDate.substring(8,16);
            anMap.put("GTEgatheringDate",startTime);
            anMap.put("LTEgatheringDate",endTime);
        }
        Page<ScfLoanService> list = this.selectPropertyByPage(anMap, anPageNum, anPageSize, 1 == anFlag);
        return list;
    }

    public int saveConfirmLoanServiceStatus(Map<String, Object> anMap) {
        Long id = Long.parseLong((String) anMap.get("id"));
        ScfLoanService scfLoanService = this.selectByPrimaryKey(id);
        String gatheringDate = (String) anMap.get("gatheringDate");
        String gatheringType = (String) anMap.get("gatheringType");
        String description = (String) anMap.get("description");
        scfLoanService.setGatheringDate(gatheringDate);
        scfLoanService.setGatheringType(gatheringType);
        scfLoanService.setDescription(description);
        scfLoanService.setBusinstatus(LoanServiceConstant.BUSINSTATUS_PREPAID);

        //将服务费信息存入分润费中
        ScfLoanShare scfLoanShare = new ScfLoanShare();
        scfLoanShare.setServiceId(scfLoanService.getLoanId());
        scfLoanShare.setCustname(scfLoanService.getCustname());   //供应商名称
        scfLoanShare.setCustcode(scfLoanService.getCustcode());   //供应商代码
        scfLoanShare.setCustno(scfLoanService.getCustno());         //供应商编号
        scfLoanShare.setCoreno(scfLoanService.getCoreno());         //核心企业编号
        scfLoanShare.setCorename(scfLoanService.getCorename());     //核心企业名称
        scfLoanShare.setCoreoperorg(scfLoanService.getCoreoperorg());   //核心企业组织机构代码
        scfLoanShare.setFactoryname(scfLoanService.getFactoryname());   //资金方名称
        scfLoanShare.setFactoryno(scfLoanService.getFactoryno());   //资金方编号

        scfLoanShare.setGatheringDate(scfLoanService.getGatheringDate());   //服务费收款日期
        scfLoanShare.setLoanBalance(scfLoanService.getLoanBalance());       //贷款项目交易金额
        scfLoanShare.setServiceCharge(scfLoanService.getServiceCharge());   //服务费金额
        scfLoanShare.setSettleaccountsType(scfLoanService.getSettleaccountsType());     //结算周期
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
        scfLoanShare.setCreatedate(sdf.format(new Date()));                 //生成日期
        scfLoanShare.setBusinstatus(LoanShareConstant.BUSINSTATUS_UNPAID);                                   //状态
//        scfLoanShare.setPayOperatorid();            //付款操作员名称
//        scfLoanShare.setPayOperatorname();          //付款操作员id


        ScfLoanShare scfLoanSharePlatform = new ScfLoanShare();
        BeanUtils.copyProperties(scfLoanShare, scfLoanSharePlatform);
        /*核心企业分润费实体插入*/
        scfLoanShare.initAdd();
        scfLoanShare.setShareScale(scfLoanService.getShareScale());         //分润比例
        BigDecimal share = scfLoanService.getShareScale().multiply(scfLoanService.getServiceCharge());
        scfLoanShare.setShare(share);
        scfLoanShare.setShareType(LoanShareConstant.SHARE_TYPE_CORE);                   //分润主体

        /*平台分润费实体插入*/
        scfLoanSharePlatform.initAdd();
        BigDecimal platformShareScale = new BigDecimal("1.00").subtract(scfLoanService.getShareScale());
        scfLoanSharePlatform.setShareScale(platformShareScale);
        scfLoanSharePlatform.setShare(scfLoanService.getServiceCharge().subtract(share));
        scfLoanSharePlatform.setShareType(LoanShareConstant.SHARE_TYPE_PLATFORM);

        scfLoanShareService.insertSelective(scfLoanShare);
        scfLoanShareService.insertSelective(scfLoanSharePlatform);

        return this.updateByPrimaryKeySelective(scfLoanService);
    }
}
